package cn.math.appmath.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import org.apache.struts2.ServletActionContext;

import cn.math.appmath.bean.ProjectBean;
import cn.math.appmath.biz.ProjectBiz;
import cn.math.appmath.entity.Project;

import com.opensymphony.xwork2.ActionSupport;

public class ProjectAction extends ActionSupport {
	private static final long serialVersionUID = -4565898601842133546L;
	private ProjectBiz projectBiz;
	private ProjectBean projectBean;
	private Project project;
	private String pname;
	private String begtime;
	private String endtime;
	private String message;
	private Integer[] nos;
	
	public String queryByObject() {
		String flag = ServletActionContext.getRequest().getParameter("flag");
		
		StringBuffer sqlStr = new StringBuffer(" where 1=1");
		ArrayList<Object> objectList = new ArrayList<Object>();
		if(flag == null){
			flag = "";
		}
		if(flag.equals("Y")){
			int page = Integer.parseInt(ServletActionContext.getRequest().getParameter("page"));
			String param = ServletActionContext.getRequest().getParameter("param");
			projectBean = this.projectBiz.query(page, param, "", null);
		}else{
			if(pname == null)
				pname = "";
			if(begtime == null)
				begtime = "";
			if(endtime == null)
				endtime = "";
			if(pname.equals("") && begtime.equals("") && endtime.equals("")) {
				projectBean = this.projectBiz.query(0, "backward", "", null);
			}else {
				DateFormat format= new SimpleDateFormat("yyyy-MM");
				if(!pname.equals("")){
					sqlStr.append(" and pname=?");
					objectList.add(pname);
				}
				if(!begtime.equals("")){
					sqlStr.append(" and ptime>=?");
					try {  
						System.out.println(format.parse(begtime));
						objectList.add(format.parse(begtime));  
			        } catch (Exception e) {  
			            e.printStackTrace();  
			        } 
				}
				if(!endtime.equals("")){
					sqlStr.append(" and ptime<=?");
					try {  
						objectList.add(format.parse(endtime));  
			        } catch (Exception e) {  
			            e.printStackTrace();  
			        } 
				}
				projectBean = this.projectBiz.query(0, null , sqlStr.toString(), objectList);
			}
		}
		return SUCCESS;
	}
	
	public String projectforward(){
		message = "";
		return SUCCESS;
	}
	
	public String findById(){
		String idStr = ServletActionContext.getRequest().getParameter("id");
		int id  = 0;
		if(!"".equals(idStr) && idStr != null)
			id = Integer.parseInt(idStr);
		project = this.projectBiz.findById(id);
		return SUCCESS;
	}
	
	/**
	 * 保存科研项目信息
	 * @return
	 */
	public String saveProject() {
		if(this.projectBiz.save(project)){
			message = "保存科研项目信息成功";
			return SUCCESS;
		}
		message = "保存科研项目信息失败";
		return INPUT;
	}
	
	/**
	 * 删除研究项目信息
	 * @return
	 */
	public String deleteProject(){
		String noStr = ServletActionContext.getRequest().getParameter("no");
		if(noStr != null && !"".equals(noStr)){
			Integer no = new Integer(noStr);
			nos = new Integer[]{no};
		}
		if(this.projectBiz.delete(nos)){
			return SUCCESS;
		}else{
			message = "删除研究项目信息失败";
			return INPUT;
		}
	}
	
	public ProjectBean getProjectBean() {
		return projectBean;
	}
	public void setProjectBean(ProjectBean projectBean) {
		this.projectBean = projectBean;
	}
	public void setProjectBiz(ProjectBiz projectBiz) {
		this.projectBiz = projectBiz;
	}

	public String getPname() {
		return pname;
	}

	public void setPname(String pname) {
		this.pname = pname;
	}

	public String getEndtime() {
		return endtime;
	}

	public void setEndtime(String endtime) {
		this.endtime = endtime;
	}

	public String getBegtime() {
		return begtime;
	}

	public void setBegtime(String begtime) {
		this.begtime = begtime;
	}

	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Integer[] getNos() {
		return nos;
	}

	public void setNos(Integer[] nos) {
		this.nos = nos;
	}
}
